package com.budwk.app.sqfwV2.component;

import com.budwk.app.sqfw.component.CommonComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.enums.SqfwFormSummaryFieldEnum;
import com.budwk.app.sqfw.models.BuildingInfo;
import com.budwk.app.sqfw.models.HouseInfo;
import com.budwk.app.sqfw.models.PersonInfo;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.BuildingInfoService;
import com.budwk.app.sqfw.services.HouseInfoService;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.util.cri.SqlExpressionGroup;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 入户
 */

public class HouseholdComponent {

    /**
     * 插入记录表
     * @param paramData
     * @param type
     * @param detailAddress
     */
    public static void insertRecord(String paramData, String type, String detailAddress) {
        SysUserService userService = NutzIocUtils.getBean(SysUserService.class);
        Sys_user user = userService.fetch(SecurityUtil.getUserId());
        Sqfw_formSummary_new formSummaryNew = new Sqfw_formSummary_new();
        formSummaryNew.setSqfwId(StrUtil.getUUID()).setUnitId(user.getUnitId())
                .setCreateDate(new Date()).setCreateBy(user.getId()).setUpdateDate(new Date()).setUpdateBy(user.getId())
                .setCreateUserName(user.getUsername()).setCreateUserPhone(user.getMobile()).setUpdateUserName(user.getUsername())
                .setUpdateUserPhone(user.getMobile()).setType(type).setUnitName(user.getUnitName()).setFillDate(new Date())
                .setProvince(user.getProvince())
                .setCity(user.getCity())
                .setDistrict(user.getDistrict())
                .setCounty(user.getCounty())
                .setCommunity(user.getCommunity())
                .setIssueStatus("0")
                .setCheckStatus(SqfwFormSummaryFieldEnum.check_status_init.getCode())
                .setParamData(paramData)
                .setInspectionSituation("1")
                .setDetailAddress(detailAddress);
        SqfwFormsummaryNewService formsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        formsummaryNewService.insert(formSummaryNew);
    }

    public static void handleUserQueryAuth(SqlExpressionGroup where) {
        Sys_user user = CommonComponent.getCurrentUser();
        // 省是直属 查询全部
        if (user.getProvince().equals(CommonConstants.DIRECTLY_UNDER)) {
            return;
        }
        where.andEquals(PersonInfo::getProvince, user.getProvince());
        if (user.getCity().equals(CommonConstants.DIRECTLY_UNDER)) {
            return;
        }
        where.andEquals(PersonInfo::getCity, user.getCity());
        if (user.getDistrict().equals(CommonConstants.DIRECTLY_UNDER)) {
            return;
        }
        where.andEquals(PersonInfo::getDistrict, user.getDistrict());
        if (user.getCounty().equals(CommonConstants.DIRECTLY_UNDER)) {
            return;
        }
        where.andEquals(PersonInfo::getCounty, user.getCounty());
        if (user.getCommunity().equals(CommonConstants.DIRECTLY_UNDER)) {
            return;
        }
        where.andEquals(PersonInfo::getCommunity, user.getCommunity());
//        // 同一单位下的用户
//        where.andEquals(PersonInfo::getUnitId, user.getUnit());
    }

    /**
     * 对导入的数据更新初始默认值
     * @param list
     */
    public static void initUpdateBuildInfo(List<BuildingInfo> list) {
        BuildingInfoService buildingInfoService = NutzIocUtils.getBean(BuildingInfoService.class);
        for (BuildingInfo buildingInfo : list) {
            boolean isUpdate = false;
            if (StringUtils.isEmpty(buildingInfo.getDetailAddress())) {
                String detailAddress = String.format("%s%s%s%s%s%s%s", StrUtil.formatStr(buildingInfo.getProvince()),
                        StrUtil.formatStr(buildingInfo.getCity()), StrUtil.formatStr(buildingInfo.getDistrict()),
                        StrUtil.formatStr(buildingInfo.getCounty()), StrUtil.formatStr(buildingInfo.getCommunity()),
                        StrUtil.formatStr(buildingInfo.getResidentialArea()), StrUtil.formatStr(buildingInfo.getBuildingName()));
                buildingInfo.setDetailAddress(detailAddress).setUpdateId(SecurityUtil.getUserId()).setUpdateTime(LocalDateTime.now());
                isUpdate = true;
            }
            if (StringUtils.isEmpty(buildingInfo.getIsFill())) {
                isUpdate = true;
                buildingInfo.setIsFill(CommonConstants.NO_CHINA);
            }
            if (isUpdate) {
                buildingInfoService.updateIgnoreNull(buildingInfo);
            }
        }
    }
    /**
     * 对导入的数据更新初始默认值
     * @param list
     */
    public static void initUpdateHouseInfo(List<HouseInfo> list, BuildingInfo buildingInfo) {
        HouseInfoService houseInfoService = NutzIocUtils.getBean(HouseInfoService.class);
        for (HouseInfo houseInfo : list) {
            if (StringUtils.isEmpty(houseInfo.getDetailAddress())) {
                String detailAddress = String.format("%s%s%s%s%s%s%s%s%s%s", StrUtil.formatStr(houseInfo.getProvince()), StrUtil.formatStr(houseInfo.getCity()),
                        StrUtil.formatStr(houseInfo.getDistrict()),
                        StrUtil.formatStr(houseInfo.getCounty()), StrUtil.formatStr(houseInfo.getCommunity()),
                        StrUtil.formatStr(buildingInfo.getResidentialArea()), StrUtil.formatStr(buildingInfo.getBuildingName()),
                        StrUtil.formatStr(houseInfo.getUnit()), StrUtil.formatStr(houseInfo.getFloor()) + "楼",
                        StrUtil.formatStr(houseInfo.getRoomNumber()));
                houseInfo.setDetailAddress(detailAddress);
                houseInfoService.updateIgnoreNull(houseInfo);
            }
        }
    }
}
